
var app = getApp()
var fun = require('../fun.js');
Page({
  data: {
    nav:{top:app.globalData.nav_top,title:'首页',home:'show',back:'show',safe_top:app.globalData.safe_top,bottom:app.globalData.safe_bottom,page:'home'},page:1,share_bt:'1',select_index:0,show_list:0
  },
  onLoad: function (op) {
    var from='direct';//从哪里来
    var from_id=op.f;if(!from_id){from_id=0;from='link';}//来源id
        if (op.scene) {//扫描
          var scene = decodeURIComponent(op.scene);
          var arrPara = scene.split("&");
          var arr = [];
          for (var i in arrPara) {
            arr = arrPara[i].split("=");
            if (arr[0] == 'f') { from_id = arr[1]; }//from_id
            if (arr[0] == 'id') { id=arr[1];}//门店id
          }
         from='scan';
        }
    app.globalData.from=from;
    app.globalData.from_id=from_id;
if(wx.getSystemInfoSync().windowHeight>680){
  this.setData({show_list:1})
}

  },
  onReady: function () {
this.load();
fun.get_pos().then(res=>{
  if(res.err=='ok'){//用户的经纬度
    app.globalData.latitude=res.latitude; 
    app.globalData.longitude=res.longitude;
  var list=this.data.list;
  if(list && app.globalData.latitude){//如果有列表，则对列表进行距离计算
    var select_hd=this.data.select_hd;
for(var i in list){
if(list[i].latitude){
var dis=fun.get_dis(app.globalData.latitude,app.globalData.longitude,list[i].latitude,list[i].longitude);
list[i].dis=dis;
if(select_hd && select_hd.id==list[i].id){
  select_hd.dis=dis;
  this.setData({select_hd:select_hd})
}
}
}
  this.setData({list:list});
  }
  }
    })


  },

  onShow: function () {

  },

  load: function () {
fun.get({ac:'get_hd_list',path:'hd'}).then(res=>{
  var select_hd;var list;
  if(res.list){
list=res.list;
if(app.globalData.latitude){//距离计算
  for(var i in list){
    if(list[i].latitude){
    var dis=fun.get_dis(app.globalData.latitude,app.globalData.longitude,list[i].latitude,list[i].longitude);
    list[i].dis=dis;
    }
    }
}
select_hd=list[0];
  }

  this.setData({
  list:list,
  user:res.user,
  //app:res.app,
  can_edit:res.can_edit,
  select_hd:select_hd,
  hd_num:res.num
  });
})


  },scroll(e){
console.log(e.detail)
var list=this.data.list;var select_index=e.detail.current
this.setData({select_index:select_index,select_hd:list[select_index]});
var num=this.data.hd_num;if(!num || num<10){return;}
var sy=num-select_index-1;//距离
if(sy<5){this.load_more();}
  },
  load_more(e){//获取更多数据
  var page=this.data.page;if(!page){page=1;}page++;
  var is_load=this.data.is_load;if(is_load==1){return;}
  var that=this;
this.setData({is_load:1});
fun.get({ac:'get_hd_list',path:'hd',c:'load_more',page:page}).then(res=>{
if(res.list){
var list=this.data.list;if(!list){list=[]}
for(var i in res.list){
if(app.globalData.latitude && res.list[i].latitude){//计算距离
  var dis=fun.get_dis(app.globalData.latitude,app.globalData.longitude,res.list[i].latitude,res.list[i].longitude);
  res.list[i].dis=dis;
}
  list.push(res.list[i])
}
var num=this.data.hd_num;if(!num){num=0}
num+=res.num;
this.setData({hd_num:num,list:list});
setTimeout(function(){that.setData({is_load:0})},200);
}

})


  },
  
  
  scroll_change(e){//滑动
   // console.log(e.detail)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },go(e){ 
    var url=e.currentTarget.dataset.url;
    if(!url){app.msg('没有设置链接，请到后台设置或者联系我们');return;}
    wx.navigateTo({url: e.currentTarget.dataset.url})},
  back(e){wx.navigateBack({delta:0})},
  home(e){ wx.reLaunch({url: '/pages/index/index'})},
  rgo(e){wx.redirectTo({url: e.currentTarget.dataset.url})},
  login: function (e) { wx.navigateTo({url: '/pages/login/index'})},
  win_open(e){var c=e.currentTarget.dataset.c;this.setData({[c]:'show'})},//打开窗口
  win_close(e){var c=e.currentTarget.dataset.c;this.setData({[c]:''})},//关闭窗口
open_min(e){wx.navigateToMiniProgram({appId: e.currentTarget.dataset.url,path: '/pages/index/index',})},
scan(e){fun.scan();},
})
